import { getSetting, chooseAddress, openSetting, showModal, showToast, requestPayment } from '../../utils/asyncWx.js'
// import regeneratorRuntime from '../../lib/runtime';
import { request } from "../../request/index.js";

Page({
  data: {
    address: {},
    cate: [],
    totalPrice: 0,
    totalNum: 0
  },
  //生命周期
  onShow() {
    //1.获取缓存中的收货地址信息
    const address = wx.getStorageSync("address");
    //获取缓存中的购物车数据
    let cart = wx.getStorageSync("cart") || [];
    //过滤后的购物车数组
    cart = cart.filter(v => v.checked);

    //1.总价格、总数量
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
      totalPrice += v.num * v.goods_price;
      totalNum += v.num;
    })
    //5,6把购物车数据重新设置回data和缓存中
    this.setData({
      cart, totalPrice, totalNum, address
    });
  },

  //点击支付功能
  async handleOrderPay() {
    try {
      //1.判断缓存中是否存在token
      const token = wx.getStorageSync("token");
      //2.判断
      if (!token) {
        wx.navigateTo({
          url: '/pages/auth/index'
        });
        return
      }
      // console.log("已经存在token");
      //3.创建订单
      //3.1准备请求头参数
      // const header = { Authorization: token };
      //3.2准备请求体参数
      const order_price = this.data.totalPrice;
      const consignee_addr = this.data.address.all;
      const { cart } = this.data;
      let goods = [];
      cart.forEach(v => goods.push({
        goods_id: v.goods_id,
        goods_number: v.num,
        goods_price: v.goods_price
      }))
      const orderParams = { order_price, consignee_addr }
      //4.准备发送请求，创建订单，获取订单编号
      const { order_number } = await request({ url: "/my/orders/create", data: orderParams });
      //5.发起预支付接口
      const { pay } = await request({ url: "/my/orders/req_unifiedorder", method: "POST", data: { order_number } });
      //6.发起微信支付
      await requestPayment(pay);
      //7.查询后台订单状态
      const res = await request({ url: "/my/orders/chkOrder", method: "POST", data: { order_number } })
      //8.弹窗提示
      await showToast({ title: "支付成功" });
      //9. 手动删除缓存中，已经支付了的商品
      let newCart = wx.getStorageSync("cart");
      //过滤已经选中的商品，只留下未选中的商品
      newCart = newCart.filter(v => !v.checked);
      //继续保存到缓存中
      wx.setStorageSync("cart", newCart);
      //10.支付成功了，跳转到订单页面
      wx.navigateTo({
        url: "/pages/order/index"
      });

    } catch (error) {
      await showToast({ title: "支付失败" });
      console.log(error);
    }
  },

})

